// miniprogram/pages/user/skills/skills.js

const app = getApp()
const db = wx.cloud.database()

import store from '../../../../store/index'
import create from '../../../../utils/create'
create(store, {
  use: ['user'],
  /**
   * 页面的初始数据
   */
  data: {
    input_code: '',
    Num: 0,
    HaveSelect: [],
    SystemSelect: ['小程序', '英语', '数学', '街舞', '社交', '领导', 'PS', 'PPT', '足球', '阅读', '绘画', '剪辑', '唱歌', '演讲', '摄影', '游泳'],
    HaveSelect2: [],
    skillValue: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      HaveSelect: store.data.user.skill,
      Num: this.data.HaveSelect.length,
    }, () => {
      console.log(this.data.HaveSelect)
    })
    this.adjust()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  DeleteSelect: function (e) {
    // console.log(e);
    var index = e.currentTarget.dataset.index;
    var HaveSelect = this.data.HaveSelect;


    HaveSelect.splice(index, 1)
    // console.log(duan)
    this.setData({
      HaveSelect,
      Num: this.data.HaveSelect.length
    })

  },
  Select: function (e) {
    // console.log(e);
    var index = parseInt(e.currentTarget.dataset.index);
    var SelectItem = this.data.SystemSelect[index];
    var len = this.data.HaveSelect.length
    console.log(index)
    // console.log(arr);
    console.log(SelectItem)
    var HaveSelect = this.data.HaveSelect
    var index1 = HaveSelect.indexOf(SelectItem)

    if (index1 >= 0) {
      wx.showToast({
        title: '已有该标签',
        icon: 'none',
        duration: 1000
      })
    }
    else if (len >= 5) {
      wx.showToast({
        title: '标签数量达上限',
        icon: 'none',
        duration: 1000
      })
    }
    else {
      this.setData({
        HaveSelect: this.data.HaveSelect.concat(SelectItem),
        len: this.data.HaveSelect.length,
      })

    }
  },
  done: function (e) {
    console.log(e)
    let len = this.data.HaveSelect.length
    if (len >= 5) {
      wx.showToast({
        title: '标签数量达上限',
        icon: 'none',
        duration: 1000
      })
    }
    else {
      this.setData({
        HaveSelect: this.data.HaveSelect.concat(this.data.skillValue),
        input_code: '',
        Num: this.data.Num + 1
      })
    }
  },
  skillInput: function (e) {
    this.setData({
      skillValue: e.detail.value
    })
  },
  handlePreserve: function () {
    const that = this
    console.log('开始更新，要将skill更新成');
    console.log(this.data.HaveSelect)
    wx.showLoading({
      title: '更新中',
    })
    db.collection('user').where({
      _openid: app.globalData.openid
    }).update({
      // data 传入需要局部更新的数据
      data: {
        // 表示将 done 字段置为 true
        skill: that.data.HaveSelect
      },
      success: function (res) {
        console.log(res)
        store.refreshUser()
      }
    })
    setTimeout(function () {
      wx.showToast({
        title: '更新成功',
      })
    }, 1000)
    this.handleBack()
  },
  handleBack: function () {
    wx.navigateBack({});
  },
  Back: function () {
    wx.navigateBack({});
  },
  adjust: function () {
    var SelectItem = this.data.SystemSelect[1];
    this.data.HaveSelect = this.data.HaveSelect.concat(SelectItem)
    // console.log("add", this.data.HaveSelect)
    this.data.HaveSelect.splice(this.data.Num, 1)
    // console.log("del", this.data.HaveSelect)

  }
})